<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/22
 * Time: 17:12
 */

namespace Mobile\Controller;
use Think\Controller;
use Think\Log;
class OrderInfoAPIController extends  Controller
{
 
    /*
     * DOTO
     * 同步澳买或澳代的订单信息
     * post 表单上传 请求 http://xxxx/orderAPI.php?s=/OrderInfoAPI/syncOrderInfo
     * 提交的参数：
     * @param orderInfo 订单的单条信息
     * 请求提交的数据格式：
     * {"order_sn":"20160223019126","consignee":"张三","address":"湖南长沙","zipcode":"410010","tel":"15236524596","mobile":"18636524596"
     * ,"email":"123@qq.com","order_type":"1","orderItemList":[{"goods_sn":"02568-pcs","goods_number":"10"},{"goods_sn":"02569-pcs","goods_number":"3"}]}
     *返回 resultCode 结果代码，1为正确，非1为失败
     *成功：
     *{"resultCode":1,"resultMsg":"订单数据同步成功。","successNum":'推送成功的个数',"failNum":'推送失败的个数'}
     *失败：
     *['resultCode':'返回小于0的数','resultMsg':'返回推送失败的原因的消息']
     * */
    public function syncOrderInfo(){
          $db_order=M('yw_order_info'); //订单主表
          $db_orderItem=M('yw_order_item'); //订单详情表
          $orderListJson=I('request.orderList','');   //批量推送数据
          Log::record("推送的订单数据：".json_encode($orderListJson));
          $token=I('request.token','','trim');  //校验token
          $orderType=I('request.send_order_type','','trim');  //订单类型
          //返回的数据
          $result['resultCode']=0;
          if($token==C('aomaitoken')||$token==C('aodaitoken')){
               if(!empty($orderListJson)){
                   $orderList=json_decode($orderListJson,true);  //解析json数据
                   $orderListTotal=count($orderList);   //批量推送的数量
                   $successNum=0;//推送成功个数
                   $successOrderIds=array();   //推送成功的订单id
                   foreach($orderList as $key=>$item){
                        //订单主表信息
                        $orderSn=$item['order_sn'];
                        $data['order_sn']=$orderSn;  //订单号
                        $data['consignee']=$item['consignee'];
                        $data['address']= $item['address'];
                        $data['zipcode']=$item['zipcode'];
                        $data['tel']= $item['tel'];
                        $data['mobile']=$item['mobile'];
                        $data['email']=$item['email'];
                        $data['order_type']=$orderType;  //推送的订单类型
                        $data['id_card']=$item['idcard'];  //省份证
                        $data['pay_time']=$item['pay_time']; //支付时间
                        $data['createtime']=time();
                        $data['photo_options']=intval($item['photo_options']);   //拍照要求参数
                        $orderItemList=$item['orderItemList'];  //订单的商品数据
                        M()->startTrans();  //开启事物
                        try{
                             //添加订单表信息
                             $orderId=0;
                             $orderResult=0;  //主订单结果
                             $findOrderInfo=$db_order->where(array('order_sn'=>$orderSn))->find();
                             if(is_array($findOrderInfo)){ //修改
                                  //更新已存在的订单信息
                                  $orderResult=$db_order->where(array('order_sn'=>$orderSn))->save($data);
                                  if($orderResult>0){
                                       $orderId=$findOrderInfo['order_id'];  //订单id
                                       //删除订单商品数据
                                       $db_orderItem->where(array('order_id'=>$orderId))->delete();
                                  }
                             }else{  //新增
                                  $orderResult=$db_order->add($data);
                                  $orderId=$orderResult;
                             }
                             if($orderId>0){
                                  $goods_item_array=array();
                                  foreach($orderItemList as $key2=>$item2){
                                       $data2['order_id']=$orderId;
                                       $data2['goods_sn']=$item2['goods_sn'];;
                                       $data2['bar_code']= $item2['bar_code'];
                                       $data2['goods_name']= $item2['goods_name'];
                                       $data2['quantity']=intval($item2['goods_number']);
                                       //2016-8-31 商品多属性内容
                                       $data2['attribute_text']=$item2['attribute_text'];
                                       array_push($goods_item_array,$data2);
                                  }
                                  $db_orderItem->addAll($goods_item_array);   //添加订单详情信息
                                  M()->commit();  //提交事物
                                  $result['resultCode']=1;
                                  $result['resultMsg']='订单数据同步成功';
                             }

                          $successNum++; //叠加已成功的个数
                          array_push($successOrderIds,$item['order_id']);  //保存推送成功的订单id
                        }catch(\Exception $ex){
                             M()->rollback();  //事物回滚
                             log::record("写入订单出错:".$ex);
                             $result['resultCode']=2;
                             $result['resultMsg']='订单数据同步失败';
                        }
                   }
                 $result['successNum']=$successNum;  //推送成功的个数
                 $result['failNum']=intval($orderListTotal)-intval($successNum);  //推送失败的个数
                 $result['orderId']=$successOrderIds;  //推送成功的ids
               }else{
                    $result['resultMsg']='同步的订单数据为空，同步失败!';
               }
          }else{
               $result['resultMsg']='token校验不一致，无权进行操作!';
               Log::record('telnet_token:'.$token." localhost_token_am:".C('aomaitoken').' localhost_token_ad:'.C('aodaitoken'));
          }
          $this->ajaxReturn($result);
     }
}